---
--- Created by root.
--- DateTime: 17-11-20 下午2:21
---

local wl = {}

wl.exefile = ""
wl.method = ""
view = nil
luashare = ngx.shared.luashare;
function wl.init(self)
    header = ngx.req.get_headers()
    config = require "config.config" --全局配置文件
    lfc = require "frame.function" --全局函数库

    request = require "frame.request" --全局请求类
    route = require (config["route_file"]) --全局路由类
    --view = require ("frame.view")
    cjson = require "cjson"
    session = require "frame.session"
    access = require "access"
    cookie = require "frame.cookie"
    factory = require "frame.factory"
    dblib = require "frame.mysql"
    db = dblib:new(config["mysql"])
    cookie = require "frame.cookie"
    cookie:update() --更新cookie的过期时间
    session:init(_) --启动sesion
    view = require ("frame.view")

    tpl_args = {}
    redis_client = nil

    if not access:run() then
        ngx.say("没有权限")
        ngx.exit(200)
    end
    return self
end

function wl.run(self)
    self.exefile,self.method = route:getExeFile()
    local content = ""
    local format = nil
    if self.method == nil then
        content = lfc.readfile(self.exefile)
        format = "html"
    else
        exeObj = require(self.exefile)

        content,format = exeObj[self.method]()

        factory:destruct() --调用对象本身colse方法，对于socket连接则使用cocosocket保持链接
    end
    db:close()
    self:send(content,format)

end

function wl.send(self, content,format)
    local html = ""
    if format == nil then
        html = cjson.encode(content)
    elseif format == "html" then
        html = content
    end
    ngx.say(html)
    --ngx.exit(200)
end

return wl
